/*
 * FrameEnviar.java
 *
 * Created on 2 de Julho de 2007, 15:59
 */
package br.com.grillo.jturbonote;

import java.awt.event.KeyEvent;
import java.net.InetAddress;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFileChooser;
import static br.com.grillo.jturbonote.Configuracao.*;
import static br.com.grillo.jturbonote.Main.*;

/**
 *
 * @author  grillo
 */
public class FrameEnviar extends javax.swing.JDialog {

    private Nota nota;
    private File anexo;
    private FrameNota frameNota;

    /** Creates new form FrameEnviar */
    public FrameEnviar() {
        initComponents();
    }

    public void setNota(FrameNota nota) {
        setNota(nota, null);
    }

    public void setNota(FrameNota frameNota, InetAddress origem) {
        this.frameNota = frameNota;
        this.nota = frameNota.getNota();
        this.txtPara.setText(getHostFormatado(origem));
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jpGeral = new javax.swing.JPanel();
        ckbApagar = new javax.swing.JCheckBox();
        jspDestino = new javax.swing.JScrollPane();
        jlDestino = new javax.swing.JList();
        txtPara = new javax.swing.JTextField();
        lbPara = new javax.swing.JLabel();
        lbArquivo = new javax.swing.JLabel();
        jbAnexar = new javax.swing.JButton();
        jbCancelar = new javax.swing.JButton();
        jbEnviar = new javax.swing.JButton();
        lbAnexo = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Enviar Nota");
        setAlwaysOnTop(true);
        setLocationByPlatform(true);
        setModal(true);

        ckbApagar.setSelected(true);
        ckbApagar.setText("Apagar ao enviar com sucesso ");
        ckbApagar.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        ckbApagar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ckbApagarActionPerformed(evt);
            }
        });

        jlDestino.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jlDestino.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jlDestinoMouseClicked(evt);
            }
        });
        jspDestino.setViewportView(jlDestino);

        txtPara.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtParaKeyPressed(evt);
            }
        });

        lbPara.setText("Para :");

        lbArquivo.setFont(new java.awt.Font("Tahoma", 1, 11));

        jbAnexar.setMnemonic('A');
        jbAnexar.setText("Anexar");
        jbAnexar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbAnexarActionPerformed(evt);
            }
        });

        jbCancelar.setMnemonic('C');
        jbCancelar.setText("Cancelar");
        jbCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbCancelarActionPerformed(evt);
            }
        });

        jbEnviar.setMnemonic('E');
        jbEnviar.setText("Enviar");
        jbEnviar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbEnviarActionPerformed(evt);
            }
        });

        lbAnexo.setText("Anexo : ");

        javax.swing.GroupLayout jpGeralLayout = new javax.swing.GroupLayout(jpGeral);
        jpGeral.setLayout(jpGeralLayout);
        jpGeralLayout.setHorizontalGroup(
            jpGeralLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpGeralLayout.createSequentialGroup()
                .addGroup(jpGeralLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(ckbApagar)
                    .addGroup(jpGeralLayout.createSequentialGroup()
                        .addGap(39, 39, 39)
                        .addComponent(jbAnexar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jbCancelar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jbEnviar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 52, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jpGeralLayout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(lbAnexo)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(lbArquivo, javax.swing.GroupLayout.PREFERRED_SIZE, 203, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(53, 53, 53))
                    .addGroup(jpGeralLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jpGeralLayout.createSequentialGroup()
                            .addGap(8, 8, 8)
                            .addComponent(lbPara)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(txtPara))
                        .addComponent(jspDestino, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 276, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        jpGeralLayout.setVerticalGroup(
            jpGeralLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpGeralLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jpGeralLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lbPara)
                    .addComponent(txtPara, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jspDestino, javax.swing.GroupLayout.PREFERRED_SIZE, 173, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(ckbApagar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jpGeralLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(lbAnexo)
                    .addComponent(lbArquivo, javax.swing.GroupLayout.DEFAULT_SIZE, 17, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jpGeralLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jbAnexar)
                    .addComponent(jbEnviar)
                    .addComponent(jbCancelar)))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jpGeral, javax.swing.GroupLayout.PREFERRED_SIZE, 281, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(54, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jpGeral, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void txtParaKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtParaKeyPressed
    if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
        jbEnviarActionPerformed(null);
    }
}//GEN-LAST:event_txtParaKeyPressed

private void jbAnexarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbAnexarActionPerformed
    String dir = getValor("ultimo-dir");
    File dirInicial;
    if (dir == null) {
        dirInicial = new File(System.getProperty("user.home"));
    } else {
        dirInicial = new File(dir);
    }
    JFileChooser fc = new JFileChooser(dirInicial);
    fc.setApproveButtonText("Selecionar");
    fc.setApproveButtonMnemonic('S');
    fc.setDialogType(JFileChooser.OPEN_DIALOG);
    fc.setMultiSelectionEnabled(false);
    fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
    fc.showOpenDialog(this);
    anexo = fc.getSelectedFile();
    if (anexo != null) {
        lbArquivo.setText(anexo.getName());
        lbArquivo.setToolTipText(anexo.getAbsolutePath());
    }
}//GEN-LAST:event_jbAnexarActionPerformed

    private void ckbApagarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ckbApagarActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_ckbApagarActionPerformed

private void jlDestinoMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jlDestinoMouseClicked
    if (jlDestino.getSelectedIndices().length == 1) {
//        System.out.println("s� tem 1");
    }

}//GEN-LAST:event_jlDestinoMouseClicked

    private void jbCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbCancelarActionPerformed
        dispose();
}//GEN-LAST:event_jbCancelarActionPerformed

private void jbEnviarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbEnviarActionPerformed
    try {
        List<String> para = new ArrayList<String>();
        para.add(txtPara.getText());

        ServicoCorreio correio = new ServicoCorreio(para, nota);
        correio.start();
        correio.join();
        dispose();
        if (ckbApagar.isSelected() && correio.isSent()) {
            frameNota.sair();
        }
//            getParent().setVisible(false);
        } catch (InterruptedException ex) {
        Logger.getLogger("global").log(Level.SEVERE, null, ex);
    }
}//GEN-LAST:event_jbEnviarActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JCheckBox ckbApagar;
    private javax.swing.JButton jbAnexar;
    private javax.swing.JButton jbCancelar;
    private javax.swing.JButton jbEnviar;
    private javax.swing.JList jlDestino;
    private javax.swing.JPanel jpGeral;
    private javax.swing.JScrollPane jspDestino;
    private javax.swing.JLabel lbAnexo;
    private javax.swing.JLabel lbArquivo;
    private javax.swing.JLabel lbPara;
    private javax.swing.JTextField txtPara;
    // End of variables declaration//GEN-END:variables
}
